const Koa = require("koa");
const Router = require("koa-router");
const mockjs = require("mockjs");
const bodyParser = require("koa-bodyparser");

const app = new Koa();
const router = new Router();
// 使用 koa-bodyparser 中间件来解析请求体
app.use(bodyParser());

const mockData = mockjs.mock({
    "data|5": [{
        "id|+1": "@id",
        checked: false,
        "name|1": ["普通会员", "高级会员"],
        "ershou|1": ["是", "否"],
        "chuzu|1": ["是", "否"],
        "chushou|1": ["是", "否"],
        "chuzu1|1": ["是", "否"],
        "shangpu|1": ["是", "否"],
        "shangzu|1": ["是", "否"],
        "nianfei|0-100": 1,
        "dengji|1-99": 1,
        "checkedtwo|1": [true, false],
    }, ],
});
// 定义路由
router.get("/api/users", async(ctx) => {
    try {
        // const movies = await mockData().data; // 异步查询电影列表
        ctx.body = mockData.data;
    } catch (err) {
        ctx.status = 500;
        ctx.body = {
            success: false,
            message: "Internal Server Error"
        };
    }
});
//丁 小区管理
const mocData = mockjs.mock({
    "data|7": [{
        "id|+1": "@id",
        checked: false,
        "paixu|100-1000": 100,
        xiaoqu: "@ctitle(4,6)",
        quyu: "@city",
        "datetime": "@datetime",
        "checkedtwo|1": [true, false],
    }, ],
});
// 定义路由
router.get("/api/QWER", async(ctx) => {
    try {
        // const movies = await mockData().data; // 异步查询电影列表
        ctx.body = mocData.data;
    } catch (err) {
        ctx.status = 500;
        ctx.body = {
            success: false,
            message: "Internal Server Error"
        };
    }
});
const contentData = mockjs.mock({ //模拟内容数据
    "data|20": [{
        "id|+1": "@id",
        "checked": false,
        "paixu|100-1000": 100,
        "name|4-6": "@cname",
        fl: "@cname",
        "key|+1": 1,

        date: "@date",
        "checkedtwo|1": [true, false],
    }, ],
})
router.get("/api/content", async(ctx) => {
    try {
        // const movies = await mockData().data; // 异步查询电影列表
        ctx.body = contentData.data;
    } catch (err) {
        ctx.status = 500;
        ctx.body = {
            success: false,
            message: "Internall Server Erbygujror"
        };
    }
});
//用户数据
const mocData1 = mockjs.mock({
    "data|5": [{
        "id|+1": "@id",
        "checked": false,
        paixu: "@date",
        "xiaoqu": "@ctitle(4,6)",
        quyu: "@cname",
        "checkedtwo|1": [true, false],
        neirong: '@ctitle(3,5)'
    }, ],
});
router.get("/api/User/bork", async(ctx) => {
        try {
            // const movies = await mockData().data; // 异步查询电影列表
            ctx.body = mocData1.data;
        } catch (err) {
            ctx.status = 500;
            ctx.body = {
                success: false,
                message: "Internal Server Error"
            };
        }
    })
    //模块数据
    //地区管理
const areaData = mockjs.mock({
    "list|10": [{
        "id|+1": 1,
        name: "@cname",
        age: "@integer(18, 60)",
        address: "@county(true)",
        "state|1": ["true", "false"],
        checked: false,
        "paixu|100-1000": 100,
        xiaoqu: "@ctitle(4,6)",
        quyu: "@city",
        date: "@date",
        "checkedtwo|1": [true, false],
    }, ],
});
// 丁的数据接口
router.get("/api/area", async(ctx) => {
    try {
        // const movies = await mockData().data; // 异步查询电影列表
        ctx.body = areaData.list;
    } catch (err) {
        ctx.status = 500;
        ctx.body = {
            success: false,
            message: "Internal Server Error"
        };
    }
});
//加盟-公司管理数据
const companyData = mockjs.mock({
        "data|8": [{
            "id|+1": 1,
            "checked": false,
            "ctitle": "@ctitle(3, 5)",
            "ctitles": "@ctitle(2, 7)",
            "cname": "@cname",
            "phone": /^1([38][0-9]|4[579]|5[0-3,5-9]|6[6]|7[0135678]|9[89])\d{8}$/,
            "region": "@region",
            "adddatetime": "@datetime",
            "now": "@now",
        }]
    })
    //学校管理
const schoolData = mockjs.mock({
    "list|100": [{
        "id|+1": 1,
        name: "@cname",
        age: "@integer(18, 60)",
        address: "@county(true)",
        "state|1": ["true", "false"],
    }, ],
});
router.get("/api/school", async(ctx) => {
    try {
        // const movies = await mockData().data; // 异步查询电影列表
        ctx.body = schoolData.list;
    } catch (err) {
        ctx.status = 500;
        ctx.body = {
            success: false,
            message: "Internal Server Error"
        };
    }
});
//推荐位管理
const recommendData = mockjs.mock({
    "list|100": [{
        "id|+1": 1,
        name: "@ctitle",
        "age|1": [
            "新房",
            "商铺出租",
            "商铺出售",
            "写字楼出租",
            "写字楼出售",
            "出租房",
            "二手房",
        ],
        address: "@county(true)",
        "state|1": ["true", "false"],
    }, ],
});
router.get("/api/recommend", async(ctx) => {
    try {
        ctx.body = recommendData.list;
    } catch (err) {
        ctx.status = 500;
        ctx.body = {
            success: false,
            message: "Internal Server Error"
        };
    }
});
//广告位管理
const adverData = mockjs.mock({
    "list|100": [{
        "id|+1": 1,
        name: "@ctitle(4,6)",
        "age|1": [
            "顶部轮播",
            "首页写字楼下方广告位",
            "首页二手出租下方广告位",
            "首页新房下方广告位",
            "首页特别推荐下方广告",
            "首页楼盘导购下方广告位",
            "首页资讯中心下方广告",
        ],
        address: "@county(true)",
        "state|1": ["true", "false"],
    }, ],
});
router.get("/api/adver", async(ctx) => {
    try {
        ctx.body = adverData.list;
    } catch (err) {
        ctx.status = 500;
        ctx.body = {
            success: false,
            message: "Internal Server Error"
        };
    }
});
//随机验证码接口
function Randomverification() {
    return mockjs.mock({
        "list|4": [{
            "sum|0-9": 1,
            "font|1": ["宋体", "楷体", "华文琥珀"],
            "jiaodu|1-90": 20,
            "color": '@color'
        }],
    });
}
router.post("/api/Randomverification", async(ctx) => {
    try {
        ctx.body = Randomverification().list;
    } catch (err) {
        ctx.status = 500;
        ctx.body = {
            success: false,
            message: "Internal Server Error"
        };
    }
});
//丁 房源管理
const Availability = mockjs.mock({
  "data|8": [{
    "id|+1": "@id(30)",
    "checked": false,
    "paixu|100-1000": 100,
    "xiaoqu": "@ctitle(4,6)",
    title: "@ctitle(10,15)",
    'mj|70-150': 70,
    'zj|70-150': 70,
    "checkedtwo|1": [true, false],
  }, ],
});
// 加盟-公司管理数据的数据接口
router.get("/api/company", async(ctx) => {
    try {
        // const movies = await mockData().data; // 异步查询电影列表
        ctx.body = companyData.data;
    } catch (err) {
        ctx.status = 500;
        ctx.body = {
            success: false,
            message: "Internal Server Error"
        };
    }
})
router.get("/api/Availability", async (ctx) => {
  try {
    // const movies = await mockData().data; // 异步查询电影列表
    ctx.body = Availability.data;
  } catch (err) {
    ctx.status = 500;
    ctx.body = {
      success: false,
      message: "Internal Server Error"
    };
  }
});
//加盟-公司管理数据
const menuData = mockjs.mock({
    "data|60": [{
        "id|+1": 1,
        "checked": false,
        "ctitle": "@ctitle(2, 6)",
        "paixu|100-1000": 100,
    }, ],
});
// 加盟-公司管理数据的数据接口
router.get("/api/menu", async(ctx) => {
    try {
        // const movies = await mockData().data; // 异步查询电影列表
        ctx.body = menuData.data;
    } catch (err) {
        ctx.status = 500;
        ctx.body = {
            success: false,
            message: "Internal Server Error"
        };
    }
})
//丁 房源管理2
const AvailabilityTow = mockjs.mock({
    "data|8": [{
        "id|+1": "@id",
        "checked": false,
        "paixu|100-1000": 100,
        "xiaoqu": "@ctitle(4,6)",
        title: "@ctitle(10,15)",
        'mj|70-150': 70,
        'zj|70-150': 70,
        "checkedtwo|1": [true, false],
    }, ],
});
router.get("/api/AvailabilityTow", async(ctx) => {
    try {
        // const movies = await mockData().data; // 异步查询电影列表
        ctx.body = AvailabilityTow.data;
    } catch (err) {
        ctx.status = 500;
        ctx.body = {
            success: false,
            message: "Internal Server Error"
        };
    }
});
//登陆
//账户密码
const arr = [{
  username: 'admin',
  passwrok: '123'
}]
router.post("/api/Longin", async (ctx) => {
  //获取前端数据
  let objName = ctx.request.body;
  //当msg存在走登录接口否则注册
  if (objName.msg) {
    //判断前端账号密码是否存在
    let pass = arr.some(item => item.username == objName.msg.username)
    //为真时执行
    if (pass) {
      arr.forEach(item => {
        //判断账号
        if (item.username == objName.msg.username) {
          //判断密码
          if (item.passwrok == objName.msg.passwrok) {
            //当expor为真进行判断为token失效时间接口
            if (objName.msg.exports) {
              let guotime = objName.msg.timese
              // 获取当前时间
              let showTime = new Date().getTime();
              //判断间隔是否大于失效时间
              if (showTime - guotime > objName.msg.exports * 1) {
                ctx.body = {
                  message: '已失效',
                };
              } else {
                ctx.body = {
                  message: '未过期',
                };
              }
            } else {
              let main = new Date().getTime()
              //登陆成功返回提示
              ctx.body = {
                message: '登陆成功',
                //过期时间设置
                exports: 86400,
                //获取登录时间戳
                timese: main
              };
            }
          } else {
            //密码错误返回
            ctx.body = {
                message: '用户名不存在',
            };
        }
    } else {
        //注册接口
        arr.push(objName.message)
        ctx.body = {
            message: '注册成功',
        };
    }})}}})

// 启动服务器
app.use(router.routes());
app.use(router.allowedMethods());
app.listen(3000, () => {
    console.log("Server is running at port 3000");
});